package com.aspirecn.core.datacache;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;

import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.commons.Constant;
import com.aspirecn.core.object.Holiday;

/**
 * 节假日数据缓存
 * @author wangxinning
 *
 */
public class HolidayCache extends BaseLocalCache {

	public final static String DATE_FORMAT = "yyyy-MM-dd";
	
	//周末范围
	
	public final static Logger logger = Logger
			.getLogger(HolidayCache.class);

	private Map<String, Holiday> holidayMap = new HashMap<String, Holiday>();

	private static HolidayCache instance = new HolidayCache();

	private HolidayCache() {

		this.dataFile = new File(Configuration.getInstance()
				.getDownloadFilePath() + File.separator + "aspiredays.xml");
		load();
	}

	public static HolidayCache getInstance() {
		return instance;
	}

	@Override
	public void load() {
		if (!loading.getAndSet(true)  && this.dataFile.exists()) {

			// 数据少，直接清
			clear();

			try {
				XMLConfiguration config = new XMLConfiguration(this.dataFile);

				int maxno = config.getMaxIndex("day");

//				<value>2013-10-01</value><!--时间值，yyyy-MM-dd-->
//				<isStockOpen>false</isStockOpen><!--股市是否开盘-->
//				<isHoliday>true</isHoliday><!--是否法定节假日-->
//				<description>国庆节</description><!--描述-->
				
				for (int i = 0; i <= maxno; i++) {

					String value = config.getString("day(" + i + ").value");
					String description = config.getString("day(" + i
							+ ").description");
					boolean isStockOpen = config.getBoolean("day(" + i + ").isStockOpen", true);
					boolean isHoliday = config.getBoolean("day(" + i + ").isHoliday", false);
					
					SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
					Holiday holiday = new Holiday();
					holiday.setDescription(StringUtils.trimToEmpty(description));
					holiday.setHoliday(isHoliday);
					holiday.setStockOpen(isStockOpen);
					holiday.setDate(sdf.parse(StringUtils.trimToEmpty(value)));
					holidayMap.put(value, holiday);
				}

				updateFileDownloadTime();

				logger.info("aspiredays配置信息加载完成");

			} catch (Exception e) {
				logger.error("aspiredays配置信息出现异常，加载失败", e);
			}

		}

		loading.set(false);
	}

	public void clear() {
		holidayMap.clear();
	}
	
	
	public Holiday search( Date date )
	{
		if( date == null )
		{
			date = new Date();
		}
		
		String key = DateFormatUtils.format(date, DATE_FORMAT);
		Holiday holiday = this.holidayMap.get(key);
		
		if( holiday == null )
		{
			holiday = new Holiday();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			
			int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
			
			if( ArrayUtils.contains(Constant.weekendrange, dayofweek) )//周六或周日
			{
				holiday.setStockOpen(false);//不开盘
				holiday.setHoliday(true);//节假日
				holiday.setDescription("周末");
			}
			else
			{
				holiday.setStockOpen(true);//不开盘
				holiday.setHoliday(false);//节假日
				holiday.setDescription("工作日");
			}
		}
		
		return holiday;
	}
	
//	public String get( String key )
//	{
//		return this.holidayMap.get(key);
//	}

	public void reload() {
		if (this.fileIsChange()) {
			this.load();
		} else {
			logger.info("没变化，aspiredays.xml信息未加载");
		}
	}
}
